﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MedicLibrary.View
{
    public partial class Form1 : Form
    {
        private DataTable dt;
        // Alternate way of maintaining a list of nodes that have already been added.
        //private List<int> doneNotes;
        private static int noteID;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DBConnector().GetDataTable("SELECT * FROM NhomThuoc");
            CreateNodes();

            foreach (TreeNode rootNode in treeView1.Nodes)
            {
                ColorNodes(rootNode, Color.MediumVioletRed, Color.DodgerBlue);
            }

            
            /*TreeNode a = new TreeNode() ;
            DBConnector dbc = new DBConnector();
            DataTable table = dbc.GetDataTable("SELECT * FROM NhomThuoc");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (Int32.Parse(table.Rows[i]["NhomChaID"].ToString()) == 0)
                    a = treeView1.Nodes.Add(table.Rows[i]["TenNhom"].ToString());
                for (int j = 0; j < table.Rows.Count; j++)
                    if (Int32.Parse(table.Rows[j]["NhomChaID"].ToString()) == i)
                        a.Nodes.Add(table.Rows[j]["TenNhom"].ToString());
            }*/
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //MessageBox.Show( treeView1.SelectedNode.Parent.FullPath);
        }

        private void CreateNodes()
        {
            DataRow[] rows = new DataRow[dt.Rows.Count];
            dt.Rows.CopyTo(rows, 0);
            //doneNotes = new List<int>(9);

            // Get the TreeView ready for node creation.
            // This isn't really needed since we're using AddRange (but it's good practice).
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();

            TreeNode[] nodes = RecurseRows(rows);
            treeView1.Nodes.AddRange(nodes);

            // Notify the TreeView to resume painting.
            treeView1.EndUpdate();
        }

        private TreeNode[] RecurseRows(DataRow[] rows)
        {
            List<TreeNode> nodeList = new List<TreeNode>();
            TreeNode node = null;

            foreach (DataRow dr in rows)
            {
                node = new TreeNode(dr["TenNhom"].ToString());
                noteID = Convert.ToInt32(dr["ID"]);

                node.Name = noteID.ToString();
                node.ToolTipText = noteID.ToString();

                // This method searches the "dirty node list" for already completed nodes.
                //if (!doneNotes.Contains(doneNoteID))

                // This alternate method using the Find method uses a Predicate generic delegate.
                if (nodeList.Find(FindNode) == null)
                {
                    DataRow[] childRows = dt.Select("NhomChaID = " + dr["ID"]);
                    if (childRows.Length > 0)
                    {
                        // Recursively call this function for all childRowsl
                        TreeNode[] childNodes = RecurseRows(childRows);

                        // Add all childnodes to this node.
                        node.Nodes.AddRange(childNodes);
                    }

                    // Mark this noteID as dirty (already added).
                    //doneNotes.Add(noteID);
                    nodeList.Add(node);
                }
            }

            // Convert this List<TreeNode> to an array so it can be added to the parent node/TreeView.
            TreeNode[] nodeArr = nodeList.ToArray();
            return nodeArr;
        }

        private static bool FindNode(TreeNode n)
        {
            if (n.Nodes.Count == 0)
                return n.Name == noteID.ToString();
            else
            {
                while (n.Nodes.Count > 0)
                {
                    foreach (TreeNode tn in n.Nodes)
                    {
                        if (tn.Name == noteID.ToString())
                            return true;
                        else
                            n = tn;
                    }
                }
                return false;
            }
        }

        protected void ColorNodes(TreeNode root, Color firstColor, Color secondColor)
        {
            root.ForeColor = root.Index % 2 == 0 ? firstColor : secondColor;

            foreach (TreeNode childNode in root.Nodes)
            {
                Color nextColor = childNode.ForeColor = childNode.Index % 2 == 0 ? firstColor : secondColor;

                if (childNode.Nodes.Count > 0)
                {
                    // alternate colors for the next node
                    if (nextColor == firstColor)
                        ColorNodes(childNode, secondColor, firstColor);
                    else
                        ColorNodes(childNode, firstColor, secondColor);
                }
            }
        }

    }
}
